package com.jjwxc.jwjskandriod.readActivity.view;

import android.graphics.Canvas;
import android.graphics.Paint;

import androidx.annotation.NonNull;

import java.util.ArrayList;

public class TxtLine {
    /** 单个字符，将来也可适用于英文 */
    private String[] words = {};
    /** 字间距 */
    private float wordGap = 0f;
    /** 当前行是否为结束行 */
    private boolean end = false;
    /** 修正偏移 */
    public static float adjust = 0f;

    /**
     * 保存每一行的字符以及字符间距
     * @param lineWords
     * @param fillSpace paint不为null表示需要计算字间距
     */
    public TxtLine(ArrayList<String> lineWords, float fillSpace) {
        words = lineWords.toArray(new String[0]);
        if (fillSpace > 0 && words.length > 1) {
            wordGap = fillSpace / (words.length - 1);
        }
    }
    public void drawText(Canvas canvas, Paint paint, int start, float top, int width) {
        start += adjust;
        for (String word : words) {
            canvas.drawText(word, start, top, paint);
            if (word.charAt(0) < 256) { // 英文
                start += (int) paint.measureText(word) + wordGap;
            } else {
                start += (int) paint.measureText("汉") + wordGap;
            }
        }
    }

    public String lineString() {
        StringBuffer sb = new StringBuffer();
        for (String w: words) {
            sb.append(w);
        }
        return sb.toString();
    }

    @NonNull
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer("间距：").append(wordGap).append("\n");
        for (String w: words) {
            sb.append(w).append(" ");
        }
        sb.append("----------------\n");
        return sb.toString();
    }

    public void setEnd(boolean end) {
        this.end = end;
        wordGap = 0f;
    }

    public boolean isEnd() {
        return end;
    }
}
